package com.supor.aiot.food.adapter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.supor.aiot.food.R;
import com.supor.aiot.food.bean.BindDeviceBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.RecyclerView;


public class RecipeDeviceAdapter extends RecyclerView.Adapter<RecipeDeviceAdapter.RecipeDeviceHolder> {
    private final List<BindDeviceBean> mList;
    private final Context mContext;
    private final onRecipeDeviceClick recipeDeviceClick;
    private int selectedPosition = -1;

    public RecipeDeviceAdapter(List<BindDeviceBean> mList, Context mContext,
                               onRecipeDeviceClick listener) {
        this.mList = mList;
        this.mContext = mContext;
        this.recipeDeviceClick = listener;
    }

    @NonNull
    @Override
    public RecipeDeviceAdapter.RecipeDeviceHolder onCreateViewHolder(@NonNull ViewGroup parent,
                                                                     int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_recipe_device, parent,
                false);
        return new RecipeDeviceHolder(view);
    }

    public void setPosition(int position) {
        this.selectedPosition = position;
        boolean isSelected = mList.get(position).isSelected();
        for (int i = 0; i < mList.size(); i++) {
            if (i == position) {
                mList.get(position).setSelected(!isSelected);
            } else {
                mList.get(i).setSelected(false);
            }
        }
        notifyDataSetChanged();
    }

    public void setPositionDevice(int position) {
        this.selectedPosition = position;
        for (int i = 0; i < mList.size(); i++) {
            if (i == position) {
                mList.get(position).setSelected(true);
            } else {
                mList.get(i).setSelected(false);
            }
        }
        notifyDataSetChanged();
    }

    //反选所有设备状态
    public void clearAll() {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).setSelected(false);
        }
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull RecipeDeviceHolder holder, int position) {
        // 禁止复用
        holder.setIsRecyclable(false);
        BindDeviceBean deviceBean = mList.get(position);
        holder.rlDeviceOuter.setOnClickListener(view -> recipeDeviceClick.OnDeviceClick(position,
                deviceBean.isSelected()));
        if (!TextUtils.isEmpty(deviceBean.getModelTypeName()))
            holder.tvDeviceName.setText(deviceBean.getModelTypeName());
        if (!TextUtils.isEmpty(deviceBean.getNote())) {
            holder.tvDeviceDesc.setText(deviceBean.getNote());
        } else {
            holder.tvDeviceDesc.setText(deviceBean.getRemark());
        }
        if (!TextUtils.isEmpty(deviceBean.getParentTypeImage()) && deviceBean.getParentTypeImage().contains("http"))
            Glide.with(mContext).load(deviceBean.getParentTypeImage()).placeholder(
                    new ColorDrawable(mContext.getResources().getColor(R.color.white_f5f5f5))).into(holder.ivDevice);
        if (deviceBean.isBind()) {
            holder.tvBind.setVisibility(View.VISIBLE);
        } else {
            holder.tvBind.setVisibility(View.GONE);
        }
        if (position == selectedPosition) {
            if (deviceBean.isSelected()) {
                holder.rlDeviceOuter.setBackground(ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.common_main_board, null));
                holder.tvDeviceName.setTextColor(mContext.getResources().getColor(R.color.normal_btn_normal));
                holder.tvDeviceDesc.setTextColor(mContext.getResources().getColor(R.color.normal_btn_normal));

                holder.tvBind.setBackground(ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.device_bind_bg, null));
                holder.tvBind.setTextColor(mContext.getResources().getColor(R.color.white));
            } else {
                holder.rlDeviceOuter.setBackground(ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.common_write_shape_bg, null));
                holder.tvDeviceName.setTextColor(mContext.getResources().getColor(R.color.black));
                holder.tvDeviceDesc.setTextColor(mContext.getResources().getColor(R.color.content_assist));

                holder.tvBind.setBackground(ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.device_bg, null));
                holder.tvBind.setTextColor(mContext.getResources().getColor(R.color.normal_btn_normal));
            }
        } else {
            holder.rlDeviceOuter.setBackground(ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.common_write_shape_bg, null));
            holder.tvDeviceName.setTextColor(mContext.getResources().getColor(R.color.black));
            holder.tvDeviceDesc.setTextColor(mContext.getResources().getColor(R.color.content_assist));

            holder.tvBind.setBackground(ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.device_bg, null));
            holder.tvBind.setTextColor(mContext.getResources().getColor(R.color.normal_btn_normal));
        }

    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class RecipeDeviceHolder extends RecyclerView.ViewHolder {
        public RelativeLayout rlDeviceOuter;
        public ImageView ivDevice;
        public TextView tvDeviceName;
        public TextView tvDeviceDesc;
        public TextView tvBind;

        public RecipeDeviceHolder(@NonNull View itemView) {
            super(itemView);
            rlDeviceOuter = itemView.findViewById(R.id.rl_device_outer);
            ivDevice = itemView.findViewById(R.id.iv_recipe_device);
            tvDeviceName = itemView.findViewById(R.id.tv_device_name);
            tvDeviceDesc = itemView.findViewById(R.id.tv_device_desc);
            tvBind = itemView.findViewById(R.id.tv_bind);
        }
    }

    public interface onRecipeDeviceClick {
        void OnDeviceClick(int position, boolean state);
    }

}
